package com.xesj.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.xesj.model.Permission;
import com.xesj.model.ReturnResult;
import com.xesj.service.PermissionServiceI;
import com.xesj.util.PageData;

/**
 * 
* @ClassName : PermissionController 
* @Description : TODO 
* @author : hnk
* @date : 2018年8月13日 下午5:43:27 
*
 */
@Controller
@RequestMapping("/permissionController")
public class PermissionController extends BaseController {
	    @Autowired
		private PermissionServiceI permissionService;
	    
	    
	    
	    @RequestMapping("/allPermissionList")
		public ModelAndView allPermissionList(){
			ModelAndView mv = getModelAndView();
			PageData pd= getPageData();
			String treenodes=null;
			pd.put("parentId", "0");
			List<PageData> permissionList=  permissionService.allPermissionList(pd);
			treenodes = JSONArray.toJSONString(permissionList);
			mv.addObject("treenodes", treenodes);
			mv.setViewName("permission/permissionEdit");
			return mv;	
		}
	    
	    
	    
	    
	    
	    
	    
	
	/**
	 * 
	* @Title : PermissionController.java 
	* @Package com.xesj.controller 
	* @Description : TODO
	* @author : hnk  
	* @date :2018年8月13日 下午5:49:32
	* @throws Exception 
	* @retutn : ModelAndView
	* @version
	 */
	@RequestMapping("/goEditPermissionPage")
	public ModelAndView goEditPermissionPage(){
		ModelAndView mv = getModelAndView();
		PageData pd= getPageData();
		PageData searchpd= getPageData();
		PageData parentpdpd= getPageData();
		String permissionId = pd.getString("permissionId");
		searchpd.put("permissionId", permissionId);
		PageData editPermissionInfo = permissionService.findPermissionById(searchpd);
		parentpdpd.put("permissionId", editPermissionInfo.getString("parentId"));
		PageData parentPermissionInfo = permissionService.findPermissionById(parentpdpd);
		mv.addObject("pd", editPermissionInfo);
		mv.addObject("pds", parentPermissionInfo);
		mv.addObject("urlmsg", "editPermission");
		mv.setViewName("permission/permissionEdit");
		return mv;	
	}
	
	
	@RequestMapping("/goAddPermissionPage")
	public ModelAndView goAddPermissionPage(){
		ModelAndView mv = getModelAndView();
		PageData pd= getPageData();
		PageData parampd= getPageData();
		String permissionId = pd.getString("permissionId");
		parampd.put("parentId", permissionId);
		PageData addPermissionInfo = permissionService.findPermissionById(pd);
		mv.addObject("pd", parampd);
		mv.addObject("pds", addPermissionInfo);
		mv.addObject("urlmsg", "savePermission");
		mv.setViewName("permission/permissionEdit");
		return mv;	
	}
	 
	@RequestMapping("/savePermission")
	@ResponseBody
	public String savePermission(Permission permission){
		PageData pd= getPageData();
		PageData result= getPageData();
		int count = permissionService.savePermission(permission);
		if (count!=0) {
			result.put("success", true);
			result.put("msg", "保存成功");
		}else {
			result.put("success", false);
			result.put("msg", "保存失败");
		}
		return JSONArray.toJSONString(result);	
	}
	
}
